<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>猜数字练习</title>
    </head>
    <body>
        <h4>已生成一个1~100之间的随机数，请猜测：</h4>
        <input type="text" id="i1">
        <button onclick="f()">确定</button>
        <h4>小提示：<span></span></h4>

        <script>
            // 生成一个1~100之间的随机数
            let num = parseInt(Math.random()*100)+1;
            console.log(num);
            let i = 0;
            function f(){
                let a = parseInt(i1.value);
                let spanE1 = document.querySelector('span');
                if (!/^\d+$/.test(a)){
                    alert("请输入数字");
                    return;
                }
                if(a>num){
                    spanE1.innerHTML = "猜大了";
                    i++;
                }else if(a<num){
                    spanE1.innerHTML = "猜小了";
                    i++;
                }else{
                    i++;
                    if (i===1){
                        spanE1.innerHTML = "nb!!!  只用了"+i+"次就猜对了";
                        return;
                    }else {
                        spanE1.innerHTML = "笨b!!!  用了"+i+"次才猜对";
                        return;
                    }
                }
            }
        </script>
    </body>
</html>